package sample.ui

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.coroutines.script.tes.systemWindowInsetsTop

class PermissionLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    private val count get() = 2
    private val spacing get() = 10f
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val leftMenuWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (74f + 40f), context.resources.displayMetrics)
        val edgeCellSpacing = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, spacing, context.resources.displayMetrics)
        val offset =  if (isInEditMode) 0 else systemWindowInsetsTop
        val viewWidth =(resources.displayMetrics.widthPixels - offset - leftMenuWidth - edgeCellSpacing * count) / count
        val viewHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, context.resources.displayMetrics)
        super.onMeasure(
            MeasureSpec.makeMeasureSpec(viewWidth.toInt(), MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(viewHeight.toInt(), MeasureSpec.EXACTLY),
        )
    }
}